package com.bkhech.home.practice.spring.lifecycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

import javax.annotation.PostConstruct;

/**
 * @Import方式导入
 */
public class MyImportBeanDefinitionRegistrar implements
        BeanNameAware,
        BeanClassLoaderAware,
        BeanFactoryAware,
        EnvironmentAware,
        InitializingBean,
        SmartInitializingSingleton,
        DisposableBean,
        ImportBeanDefinitionRegistrar {

    private String name = "MyImportBeanDefinitionRegistrar";

    static {
        System.out.println("MyImportBeanDefinitionRegistrar static");
    }

    {
        System.out.println("MyImportBeanDefinitionRegistrar block");
    }

    public MyImportBeanDefinitionRegistrar() {
        this.name = "bkhech";
        System.out.println("MyImportBeanDefinitionRegistrar constructor");
    }

    @PostConstruct
    public void initPostConstruct() {
        System.out.println("MyImportBeanDefinitionRegistrar @PostConstruct");
    }

    public void initMethod() {
        System.out.println("MyImportBeanDefinitionRegistrar initMethod");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("MyImportBeanDefinitionRegistrar setBeanFactory");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("MyImportBeanDefinitionRegistrar setBeanName---" + name);
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("MyImportBeanDefinitionRegistrar destroy");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("MyImportBeanDefinitionRegistrar afterPropertiesSet");
    }

    @Override
    public void afterSingletonsInstantiated() {
        //实例化之后执行
        System.out.println("MyImportBeanDefinitionRegistrar afterSingletonsInstantiated");
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println("MyImportBeanDefinitionRegistrar setBeanClassLoader");
    }

    @Override
    public void setEnvironment(Environment environment) {
        System.out.println("MyImportBeanDefinitionRegistrar setEnvironment");
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        System.out.println("MyImportBeanDefinitionRegistrar registerBeanDefinitions");
    }

}
